using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 租户性能指标实体
    /// 用于记录和分析租户的各项性能指标
    /// </summary>
    [Table("tenant_performance_metrics")]
    public class TenantPerformanceMetric : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 指标名称
        /// 如：response_time, cpu_usage, memory_usage等
        /// </summary>
        [Required]
        [StringLength(100)]
        public string MetricName { get; set; } = string.Empty;

        /// <summary>
        /// 指标值
        /// </summary>
        public decimal MetricValue { get; set; }

        /// <summary>
        /// 计量单位
        /// 如：ms, %, MB等
        /// </summary>
        [StringLength(20)]
        public string? Unit { get; set; }

        /// <summary>
        /// 指标类型
        /// 如：performance, usage, business等
        /// </summary>
        [StringLength(50)]
        public string MetricType { get; set; } = string.Empty;

        /// <summary>
        /// 记录时间
        /// </summary>
        public DateTime RecordedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 时间窗口
        /// 指标统计的时间窗口（分钟）
        /// </summary>
        public int TimeWindowMinutes { get; set; } = 1;

        /// <summary>
        /// 标签信息
        /// JSON格式存储额外的标签数据
        /// </summary>
        [Column(TypeName = "text")]
        public string? Tags { get; set; }

        /// <summary>
        /// 指标来源
        /// 如：api, database, cache等
        /// </summary>
        [StringLength(100)]
        public string? Source { get; set; }
    }

    /// <summary>
    /// 租户活动统计实体
    /// 用于记录租户的日常活动统计数据
    /// </summary>
    [Table("tenant_activity_statistics")]
    public class TenantActivityStatistic : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 统计日期
        /// </summary>
        public DateTime StatisticDate { get; set; }

        /// <summary>
        /// 活跃用户数
        /// </summary>
        public int ActiveUsers { get; set; }

        /// <summary>
        /// API调用次数
        /// </summary>
        public long ApiCalls { get; set; }

        /// <summary>
        /// 数据库查询次数
        /// </summary>
        public long DatabaseQueries { get; set; }

        /// <summary>
        /// 页面访问次数
        /// </summary>
        public long PageViews { get; set; }

        /// <summary>
        /// 文件上传次数
        /// </summary>
        public int FileUploads { get; set; }

        /// <summary>
        /// 上传文件总大小（字节）
        /// </summary>
        public long UploadedBytes { get; set; }

        /// <summary>
        /// 错误次数
        /// </summary>
        public int ErrorCount { get; set; }

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public decimal AverageResponseTime { get; set; }

        /// <summary>
        /// 峰值并发用户数
        /// </summary>
        public int PeakConcurrentUsers { get; set; }

        /// <summary>
        /// 存储使用量（字节）
        /// </summary>
        public long StorageUsed { get; set; }

        /// <summary>
        /// 带宽使用量（字节）
        /// </summary>
        public long BandwidthUsed { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 扩展统计数据
        /// JSON格式存储其他统计信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? ExtendedStats { get; set; }
    }

    /// <summary>
    /// 租户成本分析实体
    /// 用于分析和跟踪租户的成本信息
    /// </summary>
    [Table("tenant_cost_analysis")]
    public class TenantCostAnalysis : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 分析周期开始时间
        /// </summary>
        public DateTime PeriodStart { get; set; }

        /// <summary>
        /// 分析周期结束时间
        /// </summary>
        public DateTime PeriodEnd { get; set; }

        /// <summary>
        /// 计算成本
        /// CPU使用成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal ComputeCost { get; set; }

        /// <summary>
        /// 存储成本
        /// 数据存储成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal StorageCost { get; set; }

        /// <summary>
        /// 带宽成本
        /// 网络流量成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal BandwidthCost { get; set; }

        /// <summary>
        /// 数据库成本
        /// 数据库操作成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal DatabaseCost { get; set; }

        /// <summary>
        /// API调用成本
        /// API使用成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal ApiCost { get; set; }

        /// <summary>
        /// 其他成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal OtherCost { get; set; }

        /// <summary>
        /// 总成本
        /// </summary>
        [Column(TypeName = "decimal(10,4)")]
        public decimal TotalCost { get; set; }

        /// <summary>
        /// 货币单位
        /// </summary>
        [StringLength(10)]
        public string Currency { get; set; } = "CNY";

        /// <summary>
        /// 成本计算模式
        /// 如：usage_based, fixed_rate等
        /// </summary>
        [StringLength(50)]
        public string CostModel { get; set; } = string.Empty;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 成本明细
        /// JSON格式存储详细的成本分解
        /// </summary>
        [Column(TypeName = "text")]
        public string? CostBreakdown { get; set; }
    }
}
